<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <!--项目回款资金来源-->
      <div class="area-header">
        <span class="area-header-title">{{ $t('项目回款信息') }}</span>
      </div>
      <el-table :data="form.dealIncomeList" border class="formTable">
        <el-table-column :label="$t('回款项目')" align="center" prop="fundIncome.dealName" />
        <el-table-column :label="$t('回款时间')" align="center" prop="holderName">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.fundIncome.distributionDate) }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('本金') + $t('金额单位')" align="center" prop="amountPrincipal">
          <template slot-scope="scope">
            <span>{{ moneyFormat(scope.row.fundIncome.amountPrincipal, 'w') }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('收益') + $t('金额单位')" align="center" prop="amountIncome">
          <template slot-scope="scope">
            <span>{{ moneyFormat(scope.row.fundIncome.amountIncome, 'w') }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('分红') + $t('金额单位')" align="center" prop="amountBonus">
          <template slot-scope="scope">
            <span>{{ moneyFormat(scope.row.fundIncome.amountBonus, 'w') }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('其他') + $t('金额单位')" align="center" prop="amountOthers">
          <template slot-scope="scope">
            <span>{{ moneyFormat(scope.row.fundIncome.amountOthers, 'w') }}</span>
          </template>
        </el-table-column>
        <el-table-column
          :label="$t('操作')"
          align="center"
          width="80"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button
              :v-bind:v-has-permi="['company:holder:remove']"
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="delRowDeal(scope.$index,scope.row)"
            >{{ $t('删除') }}
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="tol" style="margin-bottom: 30px;">
        <el-button size="small" type="primary" @click="addHandle('deal')">{{ $t("新增") }}</el-button>
      </div>
      <!--其他收益资金来源-->
      <div class="area-header">
        <span class="area-header-title">{{ $t('其他资金来源') }}</span>
      </div>
      <el-table :data="form.otherIncomeList" border class="formTable">
        <el-table-column :label="$t('回款时间')" align="center" prop="holderName">
          <template slot-scope="scope">
            <span>
              <DatePicker
                v-model="scope.row.distributionDate"
                :placeholder="$t('回款时间')"
              />
            </span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('本金') + $t('金额单位')" align="center" prop="amountPrincipal">
          <template slot-scope="scope">
            <span>
              <Money v-model="scope.row.amountPrincipal" unit="w" :placeholder="$t('本金')" />
            </span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('收益') + $t('金额单位')" align="center" prop="amountIncome">
          <template slot-scope="scope">
            <span>
              <Money v-model="scope.row.amountIncome" unit="w" :placeholder="$t('收益')" />
            </span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('分红') + $t('金额单位')" align="center" prop="amountBonus">
          <template slot-scope="scope">
            <span>
              <Money v-model="scope.row.amountBonus" unit="w" :placeholder="$t('分红')" />
            </span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('其他') + $t('金额单位')" align="center" prop="amountOthers">
          <template slot-scope="scope">
            <span>
              <Money v-model="scope.row.amountOthers" unit="w" :placeholder="$t('其他')" />
            </span>
          </template>
        </el-table-column>
        <el-table-column
          :label="$t('操作')"
          align="center"
          width="80"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="delRowOther(scope.$index,scope.row)"
            >{{ $t('删除') }}
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="tol" style="margin-bottom: 30px;">
        <el-button size="small" type="primary" @click="addOtherHandle()">{{ $t("新增") }}
        </el-button>
      </div>

      <div class="area-header">
        <span class="area-header-title">{{ $t('分配基本信息') }}</span>
      </div>
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('分配批次')" prop="fpQc">
            <el-input v-model="form.fpQc" readonly :placeholder="$t('分配批次')" show-word-limit maxlength="50" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('分配时间')" prop="fpTime">
            <DatePicker
              v-model="form.fpTime"
              :placeholder="$t('分配时间')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('基金账户余额') + $t('(元)')" prop="fundAccountBalance">
            <Money v-model="form.fundAccountBalance" :placeholder="$t('基金账户余额')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('项目回款金额') + $t('(元)')" prop="projectPaymentAmount">
            <Money v-model="form.projectPaymentAmount" :placeholder="$t('项目回款金额')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('预留费用') + $t('(元)')" prop="reservedFee">
            <Money v-model="form.reservedFee" :placeholder="$t('预留费用')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('本次分配金额') + $t('(元)')" prop="amountAllocatedThis">
            <Money v-model="form.amountAllocatedThis" :placeholder="$t('本次分配金额')" @change="computationalCollection" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('备注')" prop="remark">
            <el-input v-model="form.remark" type="textarea" :placeholder="$t('备注')" show-word-limit maxlength="2000" />
          </el-form-item>
        </el-col>
      </el-row>

      <!-- 分配子表 -->
      <div class="area-header">
        <span class="area-header-title">{{ $t('分配明细') }}</span>
      </div>
      <el-table
        :data="form.fundFpSpvDetailsList"
        border
        class="nowrapTable formTable"
      >
        <el-table-column align="center" fixed="left" min-width="230px">
          <template slot="header" slot-scope="{}">
            <i style="color: #ff4949;margin-right: 4px;">*</i>
            <span>{{ $t('出资人名称') }}</span>
          </template>
          <template slot-scope="scope">
            <el-form-item :prop="'fundFpSpvDetailsList.' + scope.$index + '.investorName'" :rules="fundFpSpvDetailsListRules.investorName">
              <el-autocomplete
                v-model="scope.row.investorName"
                popper-class="my-autocomplete"
                clearable
                :disabled="scope.row.id"
                :fetch-suggestions="querySearch"
                :placeholder="$t('请输入')"
                @select="handleSelect($event, scope.row)"
                @change="handleChange(scope.row)"
                @blur="handleChange(scope.row)"
              >
                <template slot-scope="{ item }">
                  <div>{{ item.userName }}</div>
                </template>
              </el-autocomplete>
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column align="center" min-width="150px">
          <template slot="header" slot-scope="{}">
            <i style="color: #ff4949;margin-right: 4px;">*</i>
            <span>{{ $t('出资人性质') }}</span>
          </template>
          <template slot-scope="scope">
            <el-form-item :prop="'fundFpSpvDetailsList.' + scope.$index + '.investorType'" :rules="fundFpSpvDetailsListRules.investorType">
              <el-select v-model="scope.row.investorType" :placeholder="$t('出资人性质')" :disabled="scope.row.investorId || scope.row.id">
                <el-option
                  v-for="dict in [{ name: 'LP', value: 'LP' }, { name: 'GP', value: 'GP' }]"
                  :key="dict.value"
                  :label="$t(dict.name)"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column align="center" min-width="150px">
          <template slot="header" slot-scope="{}">
            <i style="color: #ff4949;margin-right: 4px;">*</i>
            <span>{{ $t('认缴金额') + $t('(元)') }}</span>
          </template>
          <template slot-scope="scope">
            <el-form-item :prop="'fundFpSpvDetailsList.' + scope.$index + '.subscribedAmount'" :rules="fundFpSpvDetailsListRules.subscribedAmount">
              <Money v-model="scope.row.subscribedAmount" :placeholder="$t('认缴金额')" :disabled="scope.row.investorId || scope.row.id" @change="computationalCollection" />
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column align="center" min-width="150px">
          <template slot="header" slot-scope="{}">
            <span>{{ $t('实缴金额') + $t('(元)') }}</span>
          </template>
          <template slot-scope="scope">
            <el-form-item :prop="'fundFpSpvDetailsList.' + scope.$index + '.amountActuallyPaid'" :rules="fundFpSpvDetailsListRules.amountActuallyPaid">
              <Money v-model="scope.row.amountActuallyPaid" :disabled="scope.row.investorId || scope.row.id" :placeholder="$t('实缴金额')" @change="computationalCollection" />
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column align="center" min-width="150px">
          <template slot="header" slot-scope="{}">
            <i style="color: #ff4949;margin-right: 4px;">*</i>
            <span>{{ $t('税费') + $t('(元)') }}</span>
          </template>
          <template slot-scope="scope">
            <el-form-item :prop="'fundFpSpvDetailsList.' + scope.$index + '.taxeFee'" :rules="fundFpSpvDetailsListRules.taxeFee">
              <Money v-model="scope.row.taxeFee" :placeholder="$t('税费')" @change="computationalCollection" />
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column align="center" min-width="200px">
          <template slot="header" slot-scope="{}">
            <i style="color: #ff4949;margin-right: 4px;">*</i>
            <span>{{ $t('本次分配金额-税前') + $t('(元)') }}</span>
            <el-tooltip :content="$t('上方本次分配金额*实缴金额/总实缴金额')" placement="top">
              <i class="el-icon-question" />
            </el-tooltip>
          </template>
          <template slot-scope="scope">
            <el-form-item :prop="'fundFpSpvDetailsList.' + scope.$index + '.preTaxDistribution'" :rules="fundFpSpvDetailsListRules.preTaxDistribution">
              <Money v-model="scope.row.preTaxDistribution" :placeholder="$t('本次分配金额-税前')" @change="computationalCollection" />
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column align="center" min-width="220px">
          <template slot="header" slot-scope="{}">
            <i style="color: #ff4949;margin-right: 4px;">*</i>
            <span>{{ $t('本次分配金额-税后') + $t('(元)') }}</span>
            <el-tooltip :content="$t('本次分配金额税前-应缴税费')" placement="top">
              <i class="el-icon-question" />
            </el-tooltip>
          </template>
          <template slot-scope="scope">
            <el-form-item :prop="'fundFpSpvDetailsList.' + scope.$index + '.afterTaxDistribution'" :rules="fundFpSpvDetailsListRules.afterTaxDistribution">
              <Money v-model="scope.row.afterTaxDistribution" :placeholder="$t('本次分配金额-税后')" @change="computationalCollection" />
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column align="center" min-width="180px">
          <template slot="header" slot-scope="{}">
            <i style="color: #ff4949;margin-right: 4px;">*</i>
            <span>{{ $t('本次分配比例') + $t('（%）') }}</span>
            <el-tooltip :content="$t('税前/实缴金额*100')" placement="top">
              <i class="el-icon-question" />
            </el-tooltip>
          </template>
          <template slot-scope="scope">
            <el-form-item :prop="'fundFpSpvDetailsList.' + scope.$index + '.scale'" :rules="fundFpSpvDetailsListRules.scale">
              <Money v-model="scope.row.scale" unit="rate" :placeholder="$t('本次分配比例')" @change="computationalCollection" />
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column align="center" min-width="150px">
          <template slot="header" slot-scope="{}">
            <span>{{ $t('累计分配金额') + $t('(元)') }}</span>
            <el-tooltip :content="$t('本次分配金额税前+历史分配给该出资人的税前分配金额合计')" placement="top">
              <i class="el-icon-question" />
            </el-tooltip>
          </template>
          <template slot-scope="scope">
            <el-form-item :prop="'fundFpSpvDetailsList.' + scope.$index + '.totalAmount'" :rules="fundFpSpvDetailsListRules.totalAmount">
              <Money v-model="scope.row.totalAmount" :placeholder="$t('累计分配金额')" />
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column align="center" min-width="170px">
          <template slot="header" slot-scope="{}">
            <span>{{ $t('累计分配比例') + $t('（%）') }}</span>
            <el-tooltip :content="$t('累计分配金额(元)/缴款金额*100')" placement="top">
              <i class="el-icon-question" />
            </el-tooltip>
          </template>
          <template slot-scope="scope">
            <el-form-item :prop="'fundFpSpvDetailsList.' + scope.$index + '.totalScale'" :rules="fundFpSpvDetailsListRules.totalScale">
              <Money v-model="scope.row.totalScale" unit="rate" :placeholder="$t('累计分配比例')" />
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column :label="$t('操作')" align="center" fixed="right" width="80">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-delete" @click="delHandler(scope.$index)">{{ $t('删除') }}</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="tol">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="addHandler">{{ $t("添加") }}</el-button>
      </div>
    </el-form>

    <!-- 选择回款数据来源 -->
    <el-dialog :title="title" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <selFundSpvIncomeList ref="selFundSpvIncomeList" />
    </el-dialog>

    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存，保存并启动流程，取消 三个按钮-->
      <SaveBtn has-permi="fund:fpSpv" />
    </div>
  </div>
</template>

<script>
import { getFundFpSpvMain, getMaxQici, saveFundFpSpvMain, getCumulativeAmountBeforeTax } from '@/api/fund/fpSpv/fundFpSpvMain'
import { delFundFpSpvSource } from '@/api/fund/fpSpv/fundFpSpvSource'
import selFundSpvIncomeList from './selFundSpvIncomeList.vue'
import { listFundInvestorAll } from '@/api/fund/investor/fundInvestor'

export default {
  components: {
    selFundSpvIncomeList
  },
  data() {
    return {
      // 分配类型字典
      fpTypeOptions: [],
      // 分配方式字典
      fpWayOptions: [],
      // 出资人分摊方式字典
      investorShareWayOptions: [],
      // // 币种字典
      // currencyOptions: [],
      // 表单参数
      form: {
        // 项目回款资金来源
        dealIncomeList: [],
        // 其他收益资金来源
        otherIncomeList: [],
        // 分配明细
        fundFpSpvDetailsList: []
      },
      // 遮罩层
      loading: false,
      // 表单校验
      rules: {
        fpTime: [{ required: true, message: this.$t('必填信息'), trigger: ['change', 'blur'] }]
      },
      fundFpSpvDetailsListRules: {
        investorName: [{ required: true, message: this.$t('必填信息'), trigger: ['change', 'blur'] }],
        investorType: [{ required: true, message: this.$t('必填信息'), trigger: ['change', 'blur'] }],
        subscribedAmount: [{ required: true, message: this.$t('必填信息'), trigger: ['change', 'blur'] }],
        preTaxDistribution: [{ required: true, message: this.$t('必填信息'), trigger: ['change', 'blur'] }],
        afterTaxDistribution: [{ required: true, message: this.$t('必填信息'), trigger: ['change', 'blur'] }],
        taxeFee: [{ required: true, message: this.$t('必填信息'), trigger: ['change', 'blur'] }],
        scale: [{ required: true, message: this.$t('必填信息'), trigger: ['change', 'blur'] }]
        // totalScore: [{ required: true, message: this.$t('必填信息'), trigger: ['change', 'blur'] }],
        // totalScale: [{ required: true, message: this.$t('必填信息'), trigger: ['change', 'blur'] }]
      },
      // 查询参数
      queryParams: {
        fundId: this.$route.query.id
      },
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      formOpen: false,
      // 投资主体数据集合
      restaurants: [],
      cumulativeAmountBeforeTax: []
    }
  },
  created() {
    this.getDicts('fund_fp_type').then(response => {
      this.fpTypeOptions = response.data
    })
    this.getDicts('fund_fp_way').then(response => {
      this.fpWayOptions = response.data
    })
    this.getDicts('fund_share_way').then(response => {
      this.investorShareWayOptions = response.data
    })
  },
  mounted() {
  },
  methods: {
    // 初始化数据
    init(data) {
      this.loading = true
      if (data != null && data.id !== undefined) {
        getFundFpSpvMain(data.id).then(response => {
          console.log(response.data)
          this.form = response.data
          this.loading = false
        })
      } else {
        this.reset()
        getMaxQici(this.queryParams).then(response => {
          this.form.fpQc = response.datas.qici
          this.loading = false
        })
        // 加载投资主体数据
        this.loadInvestmentData()
      }
      // 按出资人名称加载历史
      this.getCumulativeAmountBeforeTaxMethod((data === null || data === undefined) ? 'undefined' : data.id)
    },
    /**
     * 按出资人名称加载历史 本次分配金额税前之和, 用于计算累计分配金额
     */
    getCumulativeAmountBeforeTaxMethod(id) {
      getCumulativeAmountBeforeTax(this.$route.query.id, id).then(response => {
        this.cumulativeAmountBeforeTax = []
        console.log(response)
        if (response.data !== null) {
          this.cumulativeAmountBeforeTax = response.data
        }
      })
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        fundId: this.$route.query.id,
        fpQc: undefined,
        fundAccountBalance: undefined,
        projectPaymentAmount: undefined,
        reservedFee: undefined,
        amountAllocatedThis: undefined,
        fpTime: undefined,
        currency: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        delFlag: undefined,
        remark: undefined,
        dealIncomeList: [],
        otherIncomeList: [],
        fundFpSpvDetailsList: []
      }
      this.resetForm('form')
    },
    // 取消按钮
    cancel() {
      this.$parent.$parent.formOpen = false
      this.$parent.$parent.reset()
    },
    /** 新增按钮操作 */
    addHandle(dataSource) {
      this.formOpen = true
      this.title = this.$t('选择收益来源')
      this.$nextTick(() => {
        this.$refs.selFundSpvIncomeList.dataSource = dataSource
        this.$refs.selFundSpvIncomeList.queryParams.dataSource = dataSource
        this.$refs.selFundSpvIncomeList.getList()
      })
    },
    // 关闭form页面
    closeForm() {
      // 关闭form页面遮罩层
      this.loading = false
      // 关闭form页面
      this.$parent.$parent.formOpen = false
      // 刷新list页面
      this.$parent.$parent.getList()
    },
    // 项目回款来源
    delRowDeal(index, row) {
      const id = row.id
      if (id !== undefined && id !== '' && id != null) {
        this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
          confirmButtonText: this.$t('确定'),
          cancelButtonText: this.$t('取消'),
          type: 'warning'
        })
        .then(function() {
          return delFundFpSpvSource(id)
        }).then(() => {
          this.form.dealIncomeList.splice(index, 1)
          return this.msgSuccess(this.$t('删除成功'))
        })
        .catch(function() {})
      } else {
        this.form.dealIncomeList.splice(index, 1)
      }
    },
    // 其他回款来源
    delRowOther(index) {
      this.form.otherIncomeList.splice(index, 1)
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.loading = true
          saveFundFpSpvMain(this.form).then(response => {
            if (response.code === 200) {
              this.msgSuccess(this.$t('保存成功'))
              this.closeForm()
            } else {
              this.msgError(response.msg)
            }
            this.loading = false
          }).catch(() => {
            this.loading = false
          })
        } else {
          this.locationError(document)
        }
      })
    },
    /** 其他资金来源 新增 **/
    addOtherHandle() {
      const row = {}
      row.id = undefined
      row.distributionDate = undefined
      row.amountPrincipal = undefined
      row.amountIncome = undefined
      row.amountBonus = undefined
      row.amountOthers = undefined
      if (!(this.form.otherIncomeList instanceof Array)) {
        this.$set(this.form, 'otherIncomeList', [])
      }
      this.$nextTick(() => {
        this.form.otherIncomeList.push(row)
      })
    },
    addHandler() {
      const row = {
        id: undefined,
        investorId: undefined,
        investorName: undefined,
        investorType: undefined,
        subscribedAmount: undefined,
        amountActuallyPaid: undefined,
        preTaxDistribution: undefined,
        afterTaxDistribution: undefined,
        taxeFee: undefined,
        scale: undefined,
        totalScore: undefined,
        totalScale: undefined
      }
      if (!(this.form.fundFpSpvDetailsList instanceof Array)) {
        this.$set(this.form, 'fundFpSpvDetailsList', [])
      }
      this.$nextTick(() => {
        this.form.fundFpSpvDetailsList.push(row)
      })
    },
    delHandler(index) {
      this.form.fundFpSpvDetailsList.splice(index, 1)
    },
    // 加载投资主体数据
    loadInvestmentData() {
      listFundInvestorAll({ fundId: this.$route.query.id }).then(response => {
        this.restaurants = response.data
        // 默认带出分配明细的出资人
        if (this.restaurants !== undefined && this.restaurants !== null) {
          this.restaurants.forEach((item, index) => {
            this.addHandler()
            this.$nextTick(() => {
              this.handleSelect(item, this.form.fundFpSpvDetailsList[index])
            })
          })
        }
      })
    },
    // 选择投资主体触发方法
    querySearch(queryString, cb) {
      const restaurants = this.restaurants
      const results = queryString ? (this.restaurants.filter(item => item.userName.includes(queryString))) : restaurants
      // 调用 callback 返回建议列表的数据
      cb(results)
    },
    /**
     * 选择投资主体数据
     * @param item   选中的主体
     * @param row    当前行数据
     * @param index  当前行索引
     */
    handleSelect(item, row) {
      // 遍历判断是否存在这个投资主体
      const temp = this.form.fundFpSpvDetailsList.find(detail => detail.investorId === item.customerId)
      if (temp) {
        this.msgError(this.$t('不能重复选择投资主体，请重新选择或者输入！'))
      } else {
        // 出资人ID
        row.investorId = item.customerId
        // 出资人名称
        row.investorName = item.userName
        // 出资人性质
        console.log(item, row)
        if (item.partnerType === 'GP' || item.partnerType === 'YGGT') {
          row.investorType = 'GP'
        } else {
          row.investorType = 'LP'
        }
        // 实缴金额
        row.amountActuallyPaid = item.accumulatedPaidMount
        // 认缴金额
        row.subscribedAmount = item.subscribedAmount
        this.computationalCollection()
      }
    },
    handleChange(row) {
      if (row.investorName === undefined || row.investorName === '' || row.investorName === '') {
        row.investorId = undefined
        row.investorType = undefined
        row.amountActuallyPaid = undefined
        row.subscribedAmount = undefined
      }
    },
    // 自动计算合集
    computationalCollection() {
      if (this.form.fundFpSpvDetailsList !== undefined && this.form.fundFpSpvDetailsList !== null && this.form.fundFpSpvDetailsList.length > 0) {
        this.form.fundFpSpvDetailsList.forEach((item, index) => {
          // 计算本次分配金额-税前
          this.calculatePreTaxDistribution(item, index)
          // 本次分配金额-税后（元）
          this.calculateAfterTaxDistribution(item, index)
          // 分配比例
          this.allocationRatio(item)
          // 累计分配金额
          this.cumulativeDistributionAmount(item)
          // 累计分配比例
          this.cumulativeDistributionRatio(item)
        })
      }
    },
    // 计算本次分配金额-税前  : 上方本次分配金额*实缴金额/总实缴金额
    calculatePreTaxDistribution(row) {
      // 本次分配金额
      var amountAllocatedThis = this.checkFormatFloat(this.form.amountAllocatedThis)
      // 实缴金额
      var amountActuallyPaid = this.checkFormatFloat(row.amountActuallyPaid)
      // 总实缴金额
      var sumAmount = 0
      this.form.fundFpSpvDetailsList.forEach(item => {
        sumAmount += this.checkFormatFloat(item.amountActuallyPaid)
      })
      if (sumAmount === 0) {
        sumAmount = 1
      }
      row.preTaxDistribution = amountAllocatedThis * amountActuallyPaid / sumAmount
    },
    // 本次分配金额-税后（元） 本次分配金额税前-应缴税费
    calculateAfterTaxDistribution(row) {
      // 本次分配金额
      var preTaxDistribution = this.checkFormatFloat(row.preTaxDistribution)
      // 税费
      var taxeFee = this.checkFormatFloat(row.taxeFee)
      row.afterTaxDistribution = preTaxDistribution - taxeFee
    },
    // 分配比例 	本次分配金额税前/实缴金额*100
    allocationRatio(row) {
      // 本次分配金额-税前
      var preTaxDistribution = row.preTaxDistribution
      // 实缴金额
      var amountActuallyPaid = this.checkFormatFloat(row.amountActuallyPaid)
      if (amountActuallyPaid === 0) {
        amountActuallyPaid = 1
      }
      row.scale = preTaxDistribution / amountActuallyPaid * 100
    },
    // 累计分配金额  本次分配金额税前+历史分配给该出资人的税前分配金额合计
    cumulativeDistributionAmount(row) {
      // 本次分配金额税前
      var preTaxDistribution = row.preTaxDistribution
      const temp = this.cumulativeAmountBeforeTax.find(item => item.investorName === row.investorName)
      row.totalAmount = preTaxDistribution + 0
      if (temp) {
        row.totalAmount = preTaxDistribution + temp.preTaxDistribution
      }
    },
    // 累计分配比例  累计分配金额(元)/缴款金额*100
    cumulativeDistributionRatio(row) {
      // 累计分配金额
      var totalAmount = row.totalAmount
      // 实缴金额
      var amountActuallyPaid = this.checkFormatFloat(row.amountActuallyPaid)
      if (amountActuallyPaid === 0) {
        amountActuallyPaid = 1
      }
      row.totalScale = totalAmount / amountActuallyPaid * 100
    },
    // 工具方法
    checkFormatFloat(val) {
      if (val !== undefined && val !== null && val !== '' && !isNaN(parseFloat(val))) {
        return parseFloat(val)
      }
      return 0
    }

  }

}
</script>
